package com.chenyaowei.web.controller.member;

import com.chenyaowei.common.annotation.Log;
import com.chenyaowei.common.annotation.RepeatSubmit;
import com.chenyaowei.common.core.controller.BaseController;
import com.chenyaowei.common.core.domain.AjaxResult;
import com.chenyaowei.common.core.page.TableDataInfo;
import com.chenyaowei.common.enums.BusinessType;
import com.chenyaowei.common.utils.poi.ExcelUtil;
import com.chenyaowei.member.domain.CywMemberReceiveAddress;
import com.chenyaowei.member.service.ICywMemberReceiveAddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 会员收货地址Controller
 *
 * @author 陈耀威
 * @date 2023-12-03
 */
@RestController
@RequestMapping("/member/receiveAddress")
public class CywMemberReceiveAddressController extends BaseController
{
    @Autowired
    private ICywMemberReceiveAddressService cywMemberReceiveAddressService;

    /**
     * 查询当前用户的收货地址列表
     */
    @GetMapping("/currentUser")
    public AjaxResult listByMemberId()
    {
        List<CywMemberReceiveAddress> list = cywMemberReceiveAddressService.selectListByUserId(getUserId());
        return success(list);
    }

    /**
     * 查询会员收货地址列表
     */
//    @PreAuthorize("@ss.hasPermi('member:receiveAddress:list')")
    @GetMapping("/list")
    public TableDataInfo list(CywMemberReceiveAddress cywMemberReceiveAddress)
    {
        startPage();
        List<CywMemberReceiveAddress> list = cywMemberReceiveAddressService.selectCywMemberReceiveAddressList(cywMemberReceiveAddress);
        return getDataTable(list);
    }

    /**
     * 导出会员收货地址列表
     */
    @PreAuthorize("@ss.hasPermi('member:receiveAddress:export')")
    @Log(title = "会员收货地址", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CywMemberReceiveAddress cywMemberReceiveAddress)
    {
        List<CywMemberReceiveAddress> list = cywMemberReceiveAddressService.selectCywMemberReceiveAddressList(cywMemberReceiveAddress);
        ExcelUtil<CywMemberReceiveAddress> util = new ExcelUtil<CywMemberReceiveAddress>(CywMemberReceiveAddress.class);
        util.exportExcel(response, list, "会员收货地址数据");
    }

    /**
     * 获取会员收货地址详细信息
     */
//    @PreAuthorize("@ss.hasPermi('member:receiveAddress:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(cywMemberReceiveAddressService.selectCywMemberReceiveAddressById(id));
    }

    /**
     * 新增会员收货地址
     */
//    @PreAuthorize("@ss.hasPermi('member:receiveAddress:add')")
    @RepeatSubmit
    @Log(title = "会员收货地址", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CywMemberReceiveAddress cywMemberReceiveAddress)
    {
        cywMemberReceiveAddress.setMemberId(getUserId());
        return toAjax(cywMemberReceiveAddressService.insertCywMemberReceiveAddress(cywMemberReceiveAddress));
    }

    /**
     * 修改会员收货地址
     */
//    @PreAuthorize("@ss.hasPermi('member:receiveAddress:edit')")
    @Log(title = "会员收货地址", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CywMemberReceiveAddress cywMemberReceiveAddress)
    {
        return toAjax(cywMemberReceiveAddressService.updateCywMemberReceiveAddress(cywMemberReceiveAddress));
    }

    /**
     * 删除会员收货地址
     */
//    @PreAuthorize("@ss.hasPermi('member:receiveAddress:remove')")
    @Log(title = "会员收货地址", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(cywMemberReceiveAddressService.deleteCywMemberReceiveAddressByIds(ids));
    }
}
